import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

import NewsView from '../views/NewsView.vue'
import LoginView from '../views/LoginView.vue'

import TvComponent from '../components/TvComponent.vue'
import FilmComponent from '../components/FilmComponent.vue'
import NewsDetailComponet from '../components/NewsDetailComponet.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView
  },
  {
    path: '/login',
    name: 'login',
    component: LoginView,
    // 独享守卫
    beforeEnter: (to, from, next) => {
      console.log('独享守卫,要去'+to.path);
      next()
    }
  },
  {
    path: '/news',
    name: 'news',
    component: NewsView,
    children: [
      {
        path: '/',
        component: TvComponent
      }
      ,
      {
        path: 'tv',
        name: 'tv',
        component: TvComponent
      },
      {
        path: 'film',
        name: 'film',
        component: FilmComponent
      }
    ]

  },
  {
    path: '/news/detail/:id',
    name: 'newsdetail',
    component: NewsDetailComponet
  },
  {
    path: '/about',
    name: 'about',
    // component: AboutView
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import('../views/AboutView.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 全局路由前置守卫
router.beforeEach((to,from,next)=>{
  console.log('将要去哪',to);
  console.log('从哪来',from);
  if(to.path == '/login'){
    next();// 放行
    return;
  } 

  let isLogin = true;

  if (isLogin) {
    next();
    return;
  } 

  // 没登录，跳转到登录页面
  next('/login')
})

router.afterEach((to,from)=>{
  console.log('你是从'+from.path+'来到'+to.path);
})
export default router
